﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace lcs.Web.Controls
{
    public partial class demandPlanInfo : System.Web.UI.UserControl
    {
        public string strid = "";
        decimal mysum = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    strid = Request.Params["id"];
                    int dpid = (Convert.ToInt32(strid));
                    ShowInfo(dpid);
                }

                if (Session["demandID"] != null)
                {
                    strid = Session["demandID"].ToString();
                    int dpid = (Convert.ToInt32(strid));
                    ShowInfo(dpid);
                } 
            }
        }

        private void ShowInfo(int dpid)
        {
            lcs.BLL.demandplaninfo bll = new lcs.BLL.demandplaninfo();
            DataSet ds = bll.GetList(" and dpid=" + dpid);
            if (ds.Tables[0].Rows.Count == 1)
            {
                this.lbldepname.Text = ds.Tables[0].Rows[0]["depname"].ToString();
                this.lblprojectname.Text = ds.Tables[0].Rows[0]["projectName"].ToString();
                this.lblprojecttype.Text = ds.Tables[0].Rows[0]["projectTypeName"].ToString();
                this.lblinputtime.Text = ds.Tables[0].Rows[0]["inputtime"].ToString();
                this.hlfile.NavigateUrl = ds.Tables[0].Rows[0]["skillfile"].ToString();
                this.lblinvoicetype.Text = ds.Tables[0].Rows[0]["invoicetype"].ToString();
                this.lblusername.Text = ds.Tables[0].Rows[0]["user_name"].ToString();
                this.lblrecomsup.Text = ds.Tables[0].Rows[0]["sname"].ToString();
                lcs.BLL.demandmaterialinfo mbll = new lcs.BLL.demandmaterialinfo();
                DataSet mds = mbll.GetList(" and dpid=" + dpid);
                gridView.DataSource = mds;
                gridView.DataBind();

                DataSet ds_d = new Maticsoft.BLL.workFlow().GetFlowInfo(dpid);

                //this.GridView1.DataSource = ds_d;
                //this.GridView1.DataBind();

                StringBuilder strb = new StringBuilder("<table style='width:100%;'>");

                int count = ds_d.Tables[0].Rows.Count;//记录总数
                int pageSize = 3;//每行显示条数
                int page = count / pageSize;//总行数
                
                int cpage = 0;//当前行


                for (cpage = 1; cpage < page + 1; cpage++)
                {
                    strb.Append("<tr>");

                    for (int j = (cpage - 1)*pageSize; j <= cpage*pageSize -1; j++)
                    {
                        DataRow r = ds_d.Tables[0].Rows[j];

                        strb.Append("<td>");
                        strb.Append(r["flow_node_name"].ToString());
                        strb.Append(":");
                        strb.Append(r["user_name"].ToString());
                        strb.Append("</td>");
                    }

                    strb.Append("</tr>");
                }

                if (count % pageSize != 0)
                {
                    int cp = count % pageSize;

                    //for (int i = 0; i < cp; i++)
                    //{
                        
                    //}

                    strb.Append("<tr>");

                    for (int j = count - cp; j < count; j++)
                    {
                        DataRow r = ds_d.Tables[0].Rows[j];

                        strb.Append("<td>");
                        strb.Append(r["flow_node_name"].ToString());
                        strb.Append(":");
                        strb.Append(r["user_name"].ToString());
                        strb.Append("</td>");
                    }

                    strb.Append("</tr>");
                    
                }

                strb.Append("</table>");

                this.lti_flownode.Text = strb.ToString();

            }

        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView myrows = (DataRowView)e.Row.DataItem;
                mysum += Convert.ToDecimal(myrows[6].ToString());
            }
            // 合计
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "合计";
                e.Row.Cells[6].Text = mysum.ToString();
            }
        }

       

    }
}